package com.soshalboard.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.soshalboard.persistence.domain.Category;
import com.soshalboard.persistence.domain.Session;
import com.soshalboard.persistence.domain.User;
import com.soshalboard.persistence.domain.WishList;
import com.soshalboard.remote.SessionManager;
import com.soshalboard.remote.interfaces.APIInterface;

@Controller
@RequestMapping(value = "/category")
public class CategoryController {
	
	@Inject
	SessionManager sessionManager;	
	
	@Inject
	APIInterface api;
	
	Gson gson = new Gson();
	
	@RequestMapping(value = "/{session_id}", method = RequestMethod.GET)
    @ResponseBody
	public ResponseEntity<String> findCategories(@PathVariable("session_id") final String sessionId) {
	    ResponseEntity<String> responseEntity = null;
	    
	    Session session = checkSession(sessionId);
	    		
	    if (null == session) 
	        return new ResponseEntity<String>("Not Authorised", HttpStatus.UNAUTHORIZED);
	    
	    Map<String, List<Category>> categoryList = api.getCategories();
	    Map<String, Set<String>> categoriesAndTypes = new HashMap<String, Set<String>>();
	    categoriesAndTypes.put("types", categoryList.keySet());
	    
	    HashSet<String> valueSet = new HashSet<String>();
	    for (List value:categoryList.values()) {
	    	valueSet.addAll(value);
	    }
	    
	    categoriesAndTypes.put("categories", valueSet);
	    
	    if(categoryList != null && categoryList.size() != 0) {
	        responseEntity = new ResponseEntity<String>(gson.toJson(categoriesAndTypes), HttpStatus.OK);
	    } else {
	        responseEntity = new ResponseEntity<String>("Object Not Found", HttpStatus.NOT_FOUND);
	    }
	    
	    return responseEntity;
	}	
	
	@RequestMapping(value = "/{session_id}/{category_id}", method = RequestMethod.GET)
    @ResponseBody
	public ResponseEntity<String> findCategory(@PathVariable("session_id") final String sessionId, @PathVariable("category_id") final Integer categoryId ) {
	    ResponseEntity<String> responseEntity = null;
	    
	    Session session = checkSession(sessionId);
	    
	    if (null == session) 
	        return new ResponseEntity<String>("Not Authorised", HttpStatus.UNAUTHORIZED);
	    
	    Category category = api.getCategory(categoryId);
	    
	    if(null != category) {
	        responseEntity = new ResponseEntity<String>(gson.toJson(category), HttpStatus.OK);
	    } else {
	        responseEntity = new ResponseEntity<String>("Object Not Found", HttpStatus.NOT_FOUND);
	    }
	    
	    return responseEntity;
	}		
	
	@RequestMapping(value = "/{session_id}/wishlist", method = RequestMethod.GET)
    @ResponseBody
	public ResponseEntity<String> findWishList(
			@PathVariable("session_id") final String sessionId,
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer pageNo,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer pageSize) {
	    ResponseEntity<String> responseEntity = null;
	    
	    Session session = checkSession(sessionId);
	    
	    if (null == session) 
	        return new ResponseEntity<String>("Not Authorised", HttpStatus.UNAUTHORIZED);
	    	
	    List<WishList> wishList = api.getWishList(0, pageNo, pageSize);
	    
	    if(wishList != null) {
	        responseEntity = new ResponseEntity<String>(gson.toJson(wishList), HttpStatus.OK);
	    } else {
	        responseEntity = new ResponseEntity<String>("Object Not Found", HttpStatus.NOT_FOUND);
	    }
	    
	    return responseEntity;
	}	
	
	public Session checkSession(String sessionId) {
		if (sessionManager.getSessionMap().containsKey(sessionId)) {
			return sessionManager.getSessionMap().get(sessionId);
		} else {
			Session session = api.getSession(sessionId);
			return session;
		}
	}	
}
